package com.jh.paymentchannel.pojo.freeQuick;


import cn.jh.common.pay.client.FreeQuickRequest;
import cn.jh.common.pay.enumtype.EncryptTypeEnum;
import cn.jh.common.utils.CommonConstants;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jh.paymentchannel.pojo.enums.ChannelTag;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.isEmpty;

/**
 * 畅捷代还/提现记录表
 */
@Entity
@Table(name = "t_withdraw_order")
public class FreeQuickWithdraw implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    /**
     * 用户id
     */
    @Column(name = "user_id")
    private String userId;

    /**
     * 请求流水号
     */
    @Column(name = "request_no")
    private String requestNo;

    /**
     * 商户编号
     */
    @Column(name = "merchant_no")
    private String merchantNo;

    /**
     * 渠道编码
     */
    @Column(name = "channel_code")
    private String channelCode;

    /**
     * 银行账号
     */
    @Column(name = "bank_account_no")
    private String bankAccountNo;

    /**
     * 银行账户名
     */
    @Column(name = "bank_account_name")
    private String bankAccountName;

    /**
     * 提现金额
     */
    @Column(name = "amount")
    private String amount;

    /**
     * 银行名称
     */
    @Column(name = "bank_name")
    private String bankName;

    /**
     * 支行名称
     */
    @Column(name = "bank_sub_name")
    private String bankSubName;

    /**
     * 银行行联号
     */
    @Column(name = "bank_channel_no")
    private String bankChannelNo;

    /**
     * 银行代码
     */
    @Column(name = "bank_code")
    private String bankCode;

    /**
     * 银行代号
     */
    @Column(name = "bank_abbr")
    private String bankAbbr;

    /**
     * 银行所属省
     */
    @Column(name = "bank_province")
    private String bankProvince;

    /**
     * 银行所属市
     */
    @Column(name = "bank_city")
    private String bankCity;

    /**
     * 银行所属区域
     */
    @Column(name = "bank_area")
    private String bankArea;

    /**
     * 钱包类型
     */
    @Column(name = "wallet_type")
    private String walletType;

    /**
     * 提现流水号
     */
    @Column(name = "withdraw_request_no")
    private String withdrawRequestNo;

    /**
     * 返回编码
     * 200表示成功
     * 其他-表示失败 此字段是通信标识，非业务标识，业务是否 成功需要查看status判断
     */
    @Column(name="code")
    private String code;

    /**
     * 返回信息
     * 成功返回“操作成功”,失败返回错误原因
     */
    @Column(name="biz_msg")
    private String bizMsg;

    /**
     * 描述信息
     * 1代表请求未抛异常
     */
    @Column(name="biz_code")
    private String bizCode;

    /**
     * 交易入口
     * 1:在线收款，2：代还，3：余额提现
     */
    @Column(name = "pay_type")
    private String payType;

    /**
     * 通道类型
     * 1：商旅，2：新无卡101，3：新无卡102
     */
    @Column(name = "channel_type")
    private String channelType;

    @Column(name = "create_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Column(name = "update_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 用户费率
     */
    @Column(name = "rate")
    private BigDecimal rate;

    /**
     * 用户输入金额
     */
    @Column(name = "in_amount")
    private BigDecimal inAmount = new BigDecimal("0.00");

    /**
     * 推荐人id
     */
    @Column(name = "pre_user_id")
    private String preUserId;

    /**
     * 推荐人费率
     */
    @Column(name = "grade_rate")
    private BigDecimal gradeRate;

    /**
     * 通道类型
     */
    @Column(name = "channel_tag")
    private String channelTag;

    /**
     * 推荐人得到分润
     */
    @Column(name = "grade_amount")
    private BigDecimal gradeAmount = new BigDecimal("0.00");

    /**
     * 交易状态0:待提现,1：成功，2：失败，3：异常,4：提现中；
     */
    @Column(name = "pay_status")
    private String payStatus;

    /**
     * 回调状态
     */
    @Transient
    private Integer feeStatus;

    @Transient
    private String bindStatus;

    @JsonIgnore
    private transient String content;
    /**
     * 下发类型
     * 400  D0下发
     * 402  T1下发
     */
    @Transient
    private String xfType;

    @Transient
    private String xfSt;

    public String getBindStatus() {
        return bindStatus;
    }

    public void setBindStatus(String bindStatus) {
        this.bindStatus = bindStatus;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(String payStatus) {
        this.payStatus = payStatus;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public String getRequestNo() {
        return requestNo;
    }

    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public String getChannelCode() {
        return channelCode;
    }

    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }

    public String getBankAccountNo() {
        return bankAccountNo;
    }

    public void setBankAccountNo(String bankAccountNo) {
        this.bankAccountNo = bankAccountNo;
    }

    public String getBankAccountName() {
        return bankAccountName;
    }

    public void setBankAccountName(String bankAccountName) {
        this.bankAccountName = bankAccountName;
    }
    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getBankSubName() {
        return bankSubName;
    }

    public void setBankSubName(String bankSubName) {
        this.bankSubName = bankSubName;
    }

    public String getBankChannelNo() {
        return bankChannelNo;
    }

    public void setBankChannelNo(String bankChannelNo) {
        this.bankChannelNo = bankChannelNo;
    }

    public String getBankCode() {
        return bankCode;
    }

    public void setBankCode(String bankCode) {
        this.bankCode = bankCode;
    }

    public String getBankAbbr() {
        return bankAbbr;
    }

    public void setBankAbbr(String bankAbbr) {
        this.bankAbbr = bankAbbr;
    }

    public String getBankProvince() {
        return bankProvince;
    }

    public void setBankProvince(String bankProvince) {
        this.bankProvince = bankProvince;
    }

    public String getBankCity() {
        return bankCity;
    }

    public void setBankCity(String bankCity) {
        this.bankCity = bankCity;
    }

    public String getBankArea() {
        return bankArea;
    }

    public void setBankArea(String bankArea) {
        this.bankArea = bankArea;
    }

    public String getWalletType() {
        return walletType;
    }

    public void setWalletType(String walletType) {
        this.walletType = walletType;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getWithdrawRequestNo() {
        return withdrawRequestNo;
    }

    public void setWithdrawRequestNo(String withdrawRequestNo) {
        this.withdrawRequestNo = withdrawRequestNo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getBizMsg() {
        return bizMsg;
    }

    public void setBizMsg(String bizMsg) {
        this.bizMsg = bizMsg;
    }

    public String getBizCode() {
        return bizCode;
    }

    public void setBizCode(String bizCode) {
        this.bizCode = bizCode;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getChannelType() {
        return channelType;
    }

    public void setChannelType(String channelType) {
        this.channelType = channelType;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    public BigDecimal getInAmount() {
        return inAmount;
    }

    public void setInAmount(BigDecimal inAmount) {
        this.inAmount = inAmount;
    }

    public String getPreUserId() {
        return preUserId;
    }

    public void setPreUserId(String preUserId) {
        this.preUserId = preUserId;
    }

    public BigDecimal getGradeRate() {
        return gradeRate;
    }

    public void setGradeRate(BigDecimal gradeRate) {
        this.gradeRate = gradeRate;
    }

    public BigDecimal getGradeAmount() {
        return gradeAmount;
    }

    public void setGradeAmount(BigDecimal gradeAmount) {
        this.gradeAmount = gradeAmount;
    }

    public String getChannelTag() {
        return channelTag;
    }

    public void setChannelTag(String channelTag) {
        this.channelTag = channelTag;
    }

    public Integer getFeeStatus() {
        return feeStatus;
    }

    public void setFeeStatus(Integer feeStatus) {
        this.feeStatus = feeStatus;
    }

    public String ofChannelTag() {
        return ChannelTag.of(getChannelTag());
    }

    public String getXfType() {
        return xfType;
    }

    public void setXfType(String xfType) {
        this.xfType = xfType;
    }

    public String getXfSt() {
        return xfSt;
    }

    public void setXfSt(String xfSt) {
        this.xfSt = xfSt;
    }

    @Override
    public String toString() {
        return "FreeQuickWithdraw{" +
                "id=" + id +
                ", userId='" + userId + '\'' +
                ", requestNo='" + requestNo + '\'' +
                ", merchantNo='" + merchantNo + '\'' +
                ", channelCode='" + channelCode + '\'' +
                ", bankAccountNo='" + bankAccountNo + '\'' +
                ", bankAccountName='" + bankAccountName + '\'' +
                ", amount='" + amount + '\'' +
                ", bankName='" + bankName + '\'' +
                ", bankSubName='" + bankSubName + '\'' +
                ", bankChannelNo='" + bankChannelNo + '\'' +
                ", bankCode='" + bankCode + '\'' +
                ", bankAbbr='" + bankAbbr + '\'' +
                ", bankProvince='" + bankProvince + '\'' +
                ", bankCity='" + bankCity + '\'' +
                ", bankArea='" + bankArea + '\'' +
                ", walletType='" + walletType + '\'' +
                ", withdrawRequestNo='" + withdrawRequestNo + '\'' +
                ", code='" + code + '\'' +
                ", bizMsg='" + bizMsg + '\'' +
                ", bizCode='" + bizCode + '\'' +
                ", payType='" + payType + '\'' +
                ", channelType='" + channelType + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", rate=" + rate +
                ", inAmount=" + inAmount +
                ", preUserId='" + preUserId + '\'' +
                ", gradeRate=" + gradeRate +
                ", channelTag='" + channelTag + '\'' +
                ", gradeAmount=" + gradeAmount +
                ", payStatus='" + payStatus + '\'' +
                ", feeStatus=" + feeStatus +
                ", bindStatus='" + bindStatus + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FreeQuickWithdraw that = (FreeQuickWithdraw) o;
        return id == that.id &&
                Objects.equals(userId, that.userId) &&
                Objects.equals(requestNo, that.requestNo) &&
                Objects.equals(merchantNo, that.merchantNo) &&
                Objects.equals(channelCode, that.channelCode) &&
                Objects.equals(bankAccountNo, that.bankAccountNo) &&
                Objects.equals(bankAccountName, that.bankAccountName) &&
                Objects.equals(amount, that.amount) &&
                Objects.equals(bankName, that.bankName) &&
                Objects.equals(bankSubName, that.bankSubName) &&
                Objects.equals(bankChannelNo, that.bankChannelNo) &&
                Objects.equals(bankCode, that.bankCode) &&
                Objects.equals(bankAbbr, that.bankAbbr) &&
                Objects.equals(bankProvince, that.bankProvince) &&
                Objects.equals(bankCity, that.bankCity) &&
                Objects.equals(bankArea, that.bankArea) &&
                Objects.equals(walletType, that.walletType) &&
                Objects.equals(withdrawRequestNo, that.withdrawRequestNo) &&
                Objects.equals(code, that.code) &&
                Objects.equals(bizMsg, that.bizMsg) &&
                Objects.equals(bizCode, that.bizCode) &&
                Objects.equals(payType, that.payType) &&
                Objects.equals(channelType, that.channelType) &&
                Objects.equals(createTime, that.createTime) &&
                Objects.equals(updateTime, that.updateTime) &&
                Objects.equals(rate, that.rate) &&
                Objects.equals(inAmount, that.inAmount) &&
                Objects.equals(preUserId, that.preUserId) &&
                Objects.equals(gradeRate, that.gradeRate) &&
                Objects.equals(channelTag, that.channelTag) &&
                Objects.equals(gradeAmount, that.gradeAmount) &&
                Objects.equals(payStatus, that.payStatus) &&
                Objects.equals(feeStatus, that.feeStatus) &&
                Objects.equals(bindStatus, that.bindStatus) &&
                Objects.equals(content, that.content);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, userId, requestNo, merchantNo, channelCode, bankAccountNo, bankAccountName, amount, bankName, bankSubName, bankChannelNo, bankCode, bankAbbr, bankProvince, bankCity, bankArea, walletType, withdrawRequestNo, code, bizMsg, bizCode, payType, channelType, createTime, updateTime, rate, inAmount, preUserId, gradeRate, channelTag, gradeAmount, payStatus, feeStatus, bindStatus, content);
    }

    public static FreeQuickWithdraw emptyToString(FreeQuickWithdraw freeQuickWithdraw) {
        if (isEmpty(freeQuickWithdraw.getRequestNo()))      freeQuickWithdraw.setRequestNo("");
        if (isEmpty(freeQuickWithdraw.getAmount()))         freeQuickWithdraw.setAmount("");
        if (isEmpty(freeQuickWithdraw.getMerchantNo()))     freeQuickWithdraw.setMerchantNo("");
        if (isEmpty(freeQuickWithdraw.getUserId()))         freeQuickWithdraw.setUserId("");
        if (isEmpty(freeQuickWithdraw.getChannelCode()))    freeQuickWithdraw.setChannelCode("");
        if (isEmpty(freeQuickWithdraw.getBizMsg()))         freeQuickWithdraw.setBizMsg("");
        if (isEmpty(freeQuickWithdraw.getPayStatus()))      freeQuickWithdraw.setPayStatus("");
        if (isEmpty(freeQuickWithdraw.getWithdrawRequestNo())) freeQuickWithdraw.setWithdrawRequestNo("");
        if (isEmpty(freeQuickWithdraw.getBankAbbr()))       freeQuickWithdraw.setBankAbbr("");
        if (isEmpty(freeQuickWithdraw.getBankAccountName())) freeQuickWithdraw.setBankAccountName("");
        if (isEmpty(freeQuickWithdraw.getBankAccountNo()))  freeQuickWithdraw.setBankAccountNo("");
        if (isEmpty(freeQuickWithdraw.getBankArea()))       freeQuickWithdraw.setBankArea("");
        if (isEmpty(freeQuickWithdraw.getBankChannelNo()))  freeQuickWithdraw.setBankChannelNo("");
        if (isEmpty(freeQuickWithdraw.getBankCity()))       freeQuickWithdraw.setBankCity("");
        if (isEmpty(freeQuickWithdraw.getBankCode()))       freeQuickWithdraw.setBankCode("");
        if (isEmpty(freeQuickWithdraw.getBankName()))       freeQuickWithdraw.setBankName("");
        if (isEmpty(freeQuickWithdraw.getBankProvince()))   freeQuickWithdraw.setBankProvince("");
        if (isEmpty(freeQuickWithdraw.getBankSubName()))    freeQuickWithdraw.setBankSubName("");
        if (isEmpty(freeQuickWithdraw.getBindStatus()))     freeQuickWithdraw.setBindStatus("");
        if (isEmpty(freeQuickWithdraw.getBizCode()))        freeQuickWithdraw.setBizCode("");
        if (isEmpty(freeQuickWithdraw.getChannelTag()))     freeQuickWithdraw.setChannelTag("");
        if (isEmpty(freeQuickWithdraw.getChannelType()))    freeQuickWithdraw.setChannelType("");
        if (isEmpty(freeQuickWithdraw.getCode()))           freeQuickWithdraw.setCode("");
        if (isEmpty(freeQuickWithdraw.getContent()))        freeQuickWithdraw.setContent("");
        if (isEmpty(freeQuickWithdraw.getPayType()))        freeQuickWithdraw.setPayType("");
        if (isEmpty(freeQuickWithdraw.getPreUserId()))      freeQuickWithdraw.setPreUserId("");
        if (isEmpty(freeQuickWithdraw.getWalletType()))     freeQuickWithdraw.setWalletType("");
        if (null == freeQuickWithdraw.getUpdateTime())      freeQuickWithdraw.setUpdateTime(Calendar.getInstance().getTime());
        if (null == freeQuickWithdraw.getRate())            freeQuickWithdraw.setRate(new BigDecimal("0.00"));
        if (null == freeQuickWithdraw.getGradeRate())       freeQuickWithdraw.setGradeRate(new BigDecimal("0.00"));
        if (null == freeQuickWithdraw.getGradeAmount())     freeQuickWithdraw.setGradeAmount(new BigDecimal("0.00"));
        if (null == freeQuickWithdraw.getFeeStatus())       freeQuickWithdraw.setFeeStatus(999);
        return freeQuickWithdraw;
    }

    public void addMap(Map<String,Object> pram){
        setMerchantNo((String) pram.getOrDefault("requestNo","0" ));
        setWalletType((String) pram.getOrDefault("settlementType","0" ));
        setBankArea((String) pram.getOrDefault("bankArea", "0"));
        setBankCity((String) pram.getOrDefault("settleBankCardCity","0" ));
        setBankProvince((String) pram.getOrDefault("settleBankCardProvince", "0"));
        setBankAbbr((String)pram.getOrDefault("settleBankAbbr","0" ));
        setBankCode((String) pram.getOrDefault("abbReviation","0" ));
        setBankChannelNo((String) pram.getOrDefault("settleBankChannelNo", "0"));
        setBankSubName((String) pram.getOrDefault("settleBankSubName","0" ));
        setBankName((String) pram.getOrDefault("settleBankName","0" ));
        setBankAccountName((String) pram.getOrDefault("settleBankAccountName", "0"));
        setBankAccountNo((String) pram.getOrDefault("settleBankAccountNo", "0"));
    }

    public FreeQuickRequest transform(Map<String,Object> pram) {
        addMap(pram);
        FreeQuickRequest request = new FreeQuickRequest(CommonConstants.AGENT_NO, CommonConstants.AGENT_KEY);
        if (StringUtils.isBlank(getRequestNo())) {
            String requestNo = String.valueOf(System.currentTimeMillis());
            setRequestNo(requestNo);
        }

        //提现中
        setPayStatus("4");
        request.setSupportSign(false);
        request.setEncryptType(EncryptTypeEnum.AES);
        request.addParam("requestNo", getRequestNo());//请求流水号
        request.addParam("merchantNo", getMerchantNo());//商户编号
        request.addParam("channelCode", CommonConstants.CHANPAYNEWCARD);//渠道编码
        request.addParam("bankAccountNo", getBankAccountNo());//银行账号
        request.addParam("bankAccountName", getBankAccountName());//银行账户名
        request.addParam("bankAccountType", CommonConstants.PRIVATE);//账户类型
        request.addParam("bankName", getBankAccountName());//银行名称
        request.addParam("bankSubName", getBankSubName());//支行名称
        request.addParam("bankChannelNo", getBankChannelNo());// 银行行联号
        request.addParam("bankCode", getBankCode());//银行代码
        request.addParam("bankAbbr", getBankAbbr());//银行代号
        request.addParam("bankProvince", getBankProvince());//银行所属省
        request.addParam("bankCity", getBankCity());//银行所属市
        request.addParam("bankArea", getBankArea());//银行所属区域
        request.addParam("walletType", CommonConstants.TODAY);//钱包类型
        request.addParam("payOrderNo", "");//XSSCANPAY专用，指定代付对应的消费订单

        return request;
    }


}
